#include "VOutputInformation.h"

#include <QRegExp>
#include <QString>
#include <QDebug>
#include <QRegularExpression>
#include <QRegularExpressionMatch>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonValue>
#include <QJsonObject>

#include "VFunctions.h"
#include "VTranslate.h"
#include "VErrorWidget.h"
#include "VGlobal.h"

VOutputInformation::VOutputInformation(QString text)
{
    _text = VTranslate::decode(text);
    _type = "error";
    _line_number = get_line_number(_text);
    _path = get_path(_text);

    _error_number = err_number(_text);
    if(_error_number != -1)
    {
        _type = _std_error.value(_error_number).type;
        _text = VTranslate::decode(text.replace(QRegularExpression
                                                (_std_error.value(_error_number).source)
                                             , _std_error.value(_error_number).replace));
        return;
    }

    foreach(CustomError error, _custom_error)
    {
        if(_text.indexOf(QRegularExpression(error.content_source)) != -1)
        {
            _text = _text.replace(QRegularExpression(error.content_source), error.content_replace);
            _type = error.type;
        }
    }

}

VOutputInformation::~VOutputInformation()
{
}

QMap<int, VOutputInformation::STDError> VOutputInformation::_std_error;
QList<VOutputInformation::CustomError> VOutputInformation::_custom_error;
void VOutputInformation::init()
{
    QJsonArray errors = VFunctions::get_file_jsonDocument(VGlobal::config_dir + "/std_error.json").object().value("errors").toArray();
    foreach(QJsonValue value, errors)
    {
        QJsonObject object = value.toObject();
        int number = object.value("number").toInt();
        QString replace = object.value("replace").toString();
        QString source = object.value("source").toString();
        QString type = object.value("type").toString();

        _std_error.insert(number, STDError(source, replace, type));
    }

    QJsonArray custom_errors = VFunctions::get_file_jsonDocument(VGlobal::config_dir + "/custom_error.json").object().value("errors").toArray();
    foreach(QJsonValue value, custom_errors)
    {
        QJsonObject object = value.toObject();
        QString path = object.value("path").toString();
        QString content_replace = object.value("content_replace").toString();
        QString content_source = object.value("content_source").toString();
        QString type = object.value("type").toString();
        _custom_error.append(CustomError(content_source, content_replace, path, type));
    }
}

// 返回-1为未找到
int VOutputInformation::err_number(QString text)
{
	QRegExp reg(": (warning|error|info) (\\d+):");

	if (reg.indexIn(text) != -1)
	{
		return reg.cap(2).toInt();
	}
	return -1;
}

QString VOutputInformation::get_path(QString text)
{
    QRegularExpression reg;
    QRegularExpressionMatch match;

    reg.setPattern(".+?\\.(c|h)");
    match = reg.match(text);
    if(match.hasMatch())
        return VTranslate::decode(match.captured());

    return "";
}

int VOutputInformation::get_line_number(QString text)
{
	QRegExp reg(":(\\d+):");
	int pos = reg.indexIn(text);
	if (pos == -1)
	{
		return -1;
	}
	return reg.cap(1).toInt();

}
